package com.lanxin.oa.personnel.dao;
import com.lanxin.base.BaseDaoImpl;
import com.lanxin.bean.JcBasicdatavalue;
import com.lanxin.bean.ZzDepartment;
import com.lanxin.bean.ZzEmployee;
import com.lanxin.util.PageBean;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class DeptDaoImpl extends BaseDaoImpl implements DeptDao {

    @Override
    public List<ZzDepartment> selectDept() {
        return queryAll("from ZzDepartment");
    }

    @Override
    public List<JcBasicdatavalue> selectBasic() {
        return queryAll("from JcBasicdatavalue j where j.state=1 and j.type=1");
    }

    @Override
    public ZzDepartment selectByName(String deptName, Integer oid) {
        String hql = "from ZzDepartment j where j.deptName='" + deptName + "' ";
        ZzDepartment dept = null;
        if (oid != null) {
            hql = hql + " and j.oid=" + oid;
            dept = (ZzDepartment) selectByHQL(hql);
            if (dept != null) {
                return null;
            }
            dept = (ZzDepartment) selectByHQL("from ZzDepartment j where j.deptName='" + deptName + "' ");
            if (dept == null) {
                return null;
            }
        } else {
            return (ZzDepartment) selectByHQL(hql);
        }

        return dept;
    }

    @Override
    public List<ZzDepartment> selectDeptName() {
        return queryAll("from ZzDepartment");
    }

    @Override
    public ZzDepartment deptJob(Integer oid) {
        return (ZzDepartment) selectByHQL("from ZzDepartment z left join fetch z.roles where z.oid = " + oid);
    }

    @Override
    public List<ZzEmployee> selectEmpName(Integer deptid, Integer jobid) {
        String hql = "from ZzEmployee z where z.deptid=" + deptid;
        if (jobid != null) {
            hql = hql + "and z.jobid=" + jobid;
        }
        return queryAll(hql);
    }

    @Override
    public ZzDepartment selectDeptById(Integer oid) {
        return (ZzDepartment) selectByHQL("from ZzDepartment d left join fetch d.dept left join fetch d.emp left join fetch d.roles where d.oid=" + oid);
    }

    @Override
    public PageBean<ZzDepartment> selectDeptPage(Integer page, Integer pageSize, ZzDepartment dept) {
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        String hql = ("from ZzDepartment d left join fetch d.dept left join fetch d.emp where 1=1");
        if (dept.getDeptName() != null && !"".equals(dept.getDeptName())) {

            hql = hql + " and d.deptName like '%" + dept.getDeptName() + "%'";
        }
        if (dept.getPhone() != null && !"".equals(dept.getPhone())) {
            hql = hql + " and d.phone like '%" + dept.getPhone() + "%'";
        }
        if (dept.getAddress() != null && !"".equals(dept.getAddress())) {
            hql = hql + " and d.address like '%" + dept.getAddress() + "%'";
        }
        if (dept.getParentoid() != null) {
            hql = hql + " and d.parentoid=" + dept.getParentoid();
        }
        return super.pageFenYE(hql, page, pageSize);
    }
}

